require("AER"); require("plm"); require("devtools"); require("stargazer")
require("lme4"); require("tseries"); require("readstata13");require("foreign");require("randtests")
require("spgs");

library(devtools)

install_github("https://github.com/cran/ivpack")

require("ivpack")

remove(list=ls())

setwd("~/Dropbox/Florence_Paper/replication/data")

#setwd("/Users/sabramso/Dropbox/Florence_Paper")

library(tseries)
library(AER)
library(readstata13)

currency<-read.csv("currency_dat.csv")

currency$d1_frac<-0

currency$d2_frac<-0


currency$d1<-ifelse(is.na(currency$d1),0,currency$d1)
currency$d2<-ifelse(is.na(currency$d2),0,currency$d2)


currency$s1<-ifelse(is.na(currency$s1),0,currency$s1)
currency$s2<-ifelse(is.na(currency$s2),0,currency$s2)






for(i in 1:nrow(currency)){
  
  currency$d1_frac[i]<-as.numeric(strsplit(as.character(currency$d1_Frac),"/")[[i]][1])/as.numeric(strsplit(as.character(currency$d1_Frac),"/")[[i]][2])
  
  
  currency$d2_frac[i]<-as.numeric(strsplit(as.character(currency$d2_Frac),"/")[[i]][1])/as.numeric(strsplit(as.character(currency$d2_Frac),"/")[[i]][2])
  
  
  
  
  
}


currency$d1_frac<-ifelse(is.na(currency$d1_frac),0,currency$d1_frac)
currency$d2_frac<-ifelse(is.na(currency$d2_frac),0,currency$d2_frac)



currency$S1<-currency$s1+(as.numeric(currency$d1)+currency$d1_frac)/12


currency[5976 , ]$s2<-77

currency[8994 , ]$s2<-NA

currency$s2<-as.numeric(as.character(currency$s2))

currency$S2<-currency$s2+(as.numeric(currency$d2)+currency$d2_frac)/12


currency$date<-as.Date(paste(currency$Year,currency$Month,currency$Day,sep="/"))










currency_td<-currency[currency$Day_Type==0,]


#currency_td<-currency_td[currency_td$Year < 1416, ]



currency_td$diff_S1[2:nrow(currency_td)]<-diff(currency_td$S1)

currency_td$diff_logS1[2:nrow(currency_td)]<-diff(log(currency_td$S1))




currency_td$diff_S2[2:nrow(currency_td)]<-diff(currency_td$S2)

currency_td$diff_logS2[2:nrow(currency_td)]<-diff(log(currency_td$S2))



currency_td$time<-1:nrow(currency_td)


pdf(file="~/Dropbox/Florence_Paper/replication/figures/stationarity.pdf")

par(mfrow=c(2,1))

par(mar=c(2,5.5,2,1))


plot(currency_td[currency_td$Year > 1393, ]$date,currency_td[currency_td$Year > 1393, ]$diff_logS1,type="n",lty=1,xlab="",ylab= expression( ln( frac(p[t],p[t-1]) ) ))

grid()

lines(currency_td[currency_td$Year > 1393, ]$date,currency_td[currency_td$Year > 1393, ]$diff_logS1,lty=2,lwd=1.25)


par(mar=c(2,4.5,2,1))

plot(currency_td[currency_td$Year > 1393, ]$date,log(currency_td[currency_td$Year > 1393, ]$S1),type="n",lty=1,xlab="",ylab= expression( ln( p[t] ) ) )

grid()

lines(currency_td[currency_td$Year > 1393, ]$date,log(currency_td[currency_td$Year > 1393, ]$S1),lty=2,lwd=1.5)


dev.off()

library(tseries)


years<-seq(1391,1431,by=1)

files<-paste("tre_mag_",years,".csv",sep="")


tre_mag<-list()


for(i in 1:length(years)){
  
  tre_mag[[i]]<-read.csv(files[i])
  
  print(c(ncol(tre_mag[[i]]),i))
  
}



tre_mag<-do.call(rbind,tre_mag)

tre_mag$date<-as.Date(paste(tre_mag$YD,tre_mag$MD,tre_mag$DD,sep="/"))


dates<-unique(currency$date)

guilds<-unique(tre_mag$Guild)





tre_mag$of_q<-paste(tre_mag$OF,tre_mag$Q,sep="-")


######################################################################################################################################


gondf<-tre_mag[tre_mag$OF==1,]

gondf<-gondf[!is.na(gondf$date),]


gondf$knight<-ifelse(gondf$OC=="CAVALIERE" | gondf$OC=="MILES",1,0 )


currency_td$gondf<-NA





currency_td$sort<-ifelse(currency_td$date %in% gondf$date,1,0)




dates<-currency_td[currency_td$sort==1,]$date


dates2<-rep(NA,nrow(gondf))


for(i in 1:nrow(gondf)){
  
  
  if(gondf$MD[i]+1==13){ dates2[i]<- paste(gondf$YD[i]+1,01,01,sep="/")  }
  
  if(gondf$MD[i]+1 < 13){ dates2[i]<-paste(gondf$YD[i],gondf$MD[i]+1, 01,sep="/") }
  
  
}




gondf$date2<-as.Date(dates2)


currency_td$time_till_sort<-NA

currency_td$time_since_sort<-NA

currency_td$surname<-NA

currency_td$surname2<-NA

currency_td$gondf2<-NA

currency_td$quarter<-NA

currency_td$knight<-NA

currency_td$between_sorts<-0




for(i in 1:nrow(gondf)){
  
  currency_td$gondf<-ifelse(currency_td$date >= gondf$date2[i], gondf$Guild[i], currency_td$gondf )
  
  
  currency_td$between_sorts <-ifelse(currency_td$date < gondf$date2[i] & currency_td$date >= gondf$date[i], 1 , currency_td$between_sorts )
  
  
  
  currency_td$gondf2<-ifelse(currency_td$date >= gondf$date[i], gondf$Guild[i], currency_td$gondf2 )
  
  currency_td$knight<-ifelse(currency_td$date >= gondf$date2[i] , gondf$knight[i], currency_td$knight )
  
  
  
  
  
  
  currency_td$surname<-ifelse(currency_td$date >= gondf$date2[i], as.character(gondf$SURNAM1[i]),currency_td$surname)
  
  
  currency_td$surname2<-ifelse(currency_td$date > gondf$date2[i], gondf$SURNAM1[i],currency_td$surname2)
  
  
  currency_td$quarter<-ifelse(currency_td$date >= gondf$date2[i], gondf$Q[i], currency_td$quarter)
  
  
}



currency_td$quart_gondf<-paste(currency_td$quarter,currency_td$gondf,sep="-")



###############################


dates<-unique(gondf$date)


currency_td$group<-currency_td$time_since_sort<-currency_td$time_till_sort<-NA


for(i in 1:length(dates)){		
  
  currency_td$time_till_sort<-ifelse(currency_td$date >= dates[i] & currency_td$date <= dates[i+1], dates[i+1]- currency_td$date, currency_td$time_till_sort)
  
  
  currency_td$time_since_sort<-ifelse(currency_td$date >= dates[i] & currency_td$date <= dates[i+1],currency_td$date-dates[i], currency_td$time_since_sort)
  
  currency_td$group<-ifelse(currency_td$date >= dates2[i] & currency_td$date < dates2[i+1], i, currency_td$group)
  
  
  
}






currency_td$diff_date[2:nrow(currency_td)]<-as.numeric(diff(currency_td$date))


library(foreign)

currency_td<-currency_td[currency_td$date >= "1390-12-29" & !is.na(currency_td$date),]


#####################################################################




q1<-tre_mag[tre_mag$of_q=="8-10",]

q2<-tre_mag[tre_mag$of_q=="8-20",]

q3<-tre_mag[tre_mag$of_q=="8-30",]

q4<-tre_mag[tre_mag$of_q=="8-40",]



####################################################################################





q1_dates2<-rep(NA,nrow(q1))


for(i in 1:nrow(q1)){
  
  
  if(q1$MD[i]+1==13){ q1_dates2[i]<- paste(q1$YD[i]+1,01,01,sep="/")  }
  
  if(q1$MD[i]+1 < 13){ q1_dates2[i]<-paste(q1$YD[i],q1$MD[i]+1, 01,sep="/") }
  
  
}




q1$date2<-q1_dates2


q1date<-unique(q1$date2)


q1l<-list()

for(i in 1:length(q1date)){
  
  
  sub<-q1[q1$date2==q1date[i],]
  
  nr<-nrow(sub)
  
  
  sub$num<-1:nr
  
  q1l[[i]]<-sub
  
  
  
}


q1<-do.call(rbind,q1l)

q11<-q1[q1$num==1,]
q12<-q1[q1$num==2,]


q11a<-q11[q11$MD %in% c(2,4,6,8,10,12)==T,]
q12a<-q12[q12$MD %in% c(2,4,6,8,10,12)==T,]


q11b<-q11[q11$MD %in% c(2,4,6,8,10,12)==F,]

q11b$date2<-paste(q11b$YD,q11b$MD,q11b$DD,sep="/")


q11c<-rbind(q11a,q11b)

q11a<-q11c[order(q11c$date2),]




##################################################





q2_dates2<-rep(NA,nrow(q2))


for(i in 1:nrow(q2)){
  
  
  if(q2$MD[i]+1==13){ q2_dates2[i]<- paste(q2$YD[i]+1,01,01,sep="/")  }
  
  if(q2$MD[i]+1 < 13){ q2_dates2[i]<-paste(q2$YD[i],q2$MD[i]+1, 01,sep="/") }
  
  
}




q2$date2<-q2_dates2


q2date<-unique(q2$date2)


q2l<-list()

for(i in 1:length(q2date)){
  
  
  sub<-q2[q2$date2==q2date[i],]
  
  nr<-nrow(sub)
  
  
  sub$num<-1:nr
  
  q2l[[i]]<-sub
  
  
  
}


q2<-do.call(rbind,q2l)

q21<-q2[q2$num==1,]
q22<-q2[q2$num==2,]


q21a<-q21[q21$MD %in% c(2,4,6,8,10,12)==T,]
q22a<-q22[q22$MD %in% c(2,4,6,8,10,12)==T,]


q21b<-q21[q21$MD %in% c(2,4,6,8,10,12)==F,]

q21b$date2<-paste(q21b$YD,q21b$MD,q21b$DD,sep="/")

q22b<-q21b[1,]

q21b<-q21b[2,]


q21c<-rbind(q21a,q21b)

q21a<-q21c[order(q21c$date2),]



q22c<-rbind(q22a,q22b)

q22a<-q22c[order(q22c$date2),]





#################################################




q3_dates2<-rep(NA,nrow(q3))


for(i in 1:nrow(q3)){
  
  
  if(q3$MD[i]+1==13){ q3_dates2[i]<- paste(q3$YD[i]+1,01,01,sep="/")  }
  
  if(q3$MD[i]+1 < 13){ q3_dates2[i]<-paste(q3$YD[i],q3$MD[i]+1, 01,sep="/") }
  
  
}




q3$date2<-q3_dates2


q3date<-unique(q3$date2)


q3l<-list()

for(i in 1:length(q3date)){
  
  
  sub<-q3[q3$date2==q3date[i],]
  
  nr<-nrow(sub)
  
  
  sub$num<-1:nr
  
  q3l[[i]]<-sub
  
  
  
}


q3<-do.call(rbind,q3l)

q31<-q3[q3$num==1,]
q32<-q3[q3$num==2,]


q31a<-q31
q32a<-q32


#################################################
q4_dates2<-rep(NA,nrow(q4))


for(i in 1:nrow(q4)){
  
  
  if(q4$MD[i]+1==13){ q4_dates2[i]<- paste(q4$YD[i]+1,01,01,sep="/")  }
  
  if(q4$MD[i]+1 < 13){ q4_dates2[i]<-paste(q4$YD[i],q4$MD[i]+1, 01,sep="/") }
  
  
}




q4$date2<-q4_dates2


q4date<-unique(q4$date2)


q4l<-list()

for(i in 1:length(q4date)){
  
  
  sub<-q4[q4$date2==q4date[i],]
  
  nr<-nrow(sub)
  
  
  sub$num<-1:nr
  
  q4l[[i]]<-sub
  
  
  
}


q4<-do.call(rbind,q4l)

q41<-q4[q4$num==1,]
q42<-q4[q4$num==2,]


q41a<-q41[q41$MD %in% c(2,4,6,8,10,12)==T,]
q42a<-q42[q42$MD %in% c(2,4,6,8,10,12)==T,]


q41b<-q41[q41$MD %in% c(2,4,6,8,10,12)==F,]

q41b$date2<-paste(q41b$YD,q41b$MD,q41b$DD,sep="/")


q41c<-rbind(q41a,q41b)

q41a<-q41c[order(q41c$date2),]





#################################################





currency_td$q1_1<-NA






for(i in 1:nrow(q11a)){
  
  currency_td$q1_1<-ifelse(currency_td$date >= q11a$date2[i], q11a[i,]$Guild, currency_td$q1_1 )
  
}







currency_td$q1_2<-NA






for(i in 1:nrow(q12a)){
  
  currency_td$q1_2<-ifelse(currency_td$date >= q12a$date2[i], q12a[i,]$Guild, currency_td$q1_2 )
  
}


#################################################


currency_td$q2_1<-NA






for(i in 1:nrow(q21a)){
  
  currency_td$q2_1<-ifelse(currency_td$date >= q21a$date2[i], q21a[i,]$Guild, currency_td$q2_1 )
  
}







currency_td$q2_2<-NA






for(i in 1:nrow(q22a)){
  
  currency_td$q2_2<-ifelse(currency_td$date >= q22a$date2[i], q22a[i,]$Guild, currency_td$q2_2 )
  
}




################################################################
currency_td$q3_1<-NA






for(i in 1:nrow(q31a)){
  
  currency_td$q3_1<-ifelse(currency_td$date >= q31a$date2[i], q31a[i,]$Guild, currency_td$q3_1 )
  
}







currency_td$q3_2<-NA






for(i in 1:nrow(q32a)){
  
  currency_td$q3_2<-ifelse(currency_td$date >= q32a$date2[i], q32a[i,]$Guild, currency_td$q3_2 )
  
}





################################################################
currency_td$q4_1<-NA






for(i in 1:nrow(q41a)){
  
  currency_td$q4_1<-ifelse(currency_td$date >= q41a$date2[i], q41a[i,]$Guild, currency_td$q4_1 )
  
}







currency_td$q4_2<-NA






for(i in 1:nrow(q42a)){
  
  currency_td$q4_2<-ifelse(currency_td$date >= q42a$date2[i], q42a[i,]$Guild, currency_td$q4_2 )
  
}


################################################################


glds<-unique(c(currency_td$gondf,currency_td$q1_1,currency_td$q1_2,currency_td$q2_1,currency_td$q2_2,currency_td$q3_1,currency_td$q3_2,currency_td$q4_1,currency_td$q4_2))


glds<-glds[!is.na(glds)]

glds<-glds[order(glds)]



guild_mat<-data.frame(matrix(0, nrow(currency_td),length(glds)))

names(guild_mat)<-glds




cols<-c("gondf","q1_1","q1_2","q2_1","q2_2","q3_1","q3_2","q4_1","q4_2")




for(i in 1:length(cols)){
  
  for(j in 1:ncol(guild_mat)){
    
    
    
    tf<-as.numeric(currency_td[,cols[i] ])==as.numeric(names(guild_mat)[j])
    
    tf<-ifelse(is.na(tf),FALSE,tf)
    
    
    
    guild_mat[,j]<-ifelse(tf, as.numeric(guild_mat[,j])+1 , as.numeric(guild_mat[,j]) )
    
    
  }
  
}




names(guild_mat)<-paste("gld",names(guild_mat),sep="_")


guild_mat<-guild_mat[,c("gld_21","gld_22","gld_23","gld_24","gld_25","gld_26","gld_27")]












currency_td<-data.frame(currency_td,guild_mat)




scrutinies<-c(1385, 1391, 1393,  1398,  1400,1406,1411,1414)



currency_td$scrutiny<-NA

for(i in 1:length(scrutinies)){
  
  
  currency_td$scrutiny<-ifelse(currency_td$Year > scrutinies[i],scrutinies[i],currency_td$scrutiny)	
  
  
}



currency_td$gondf<-ifelse(currency_td$gondf==27,24,currency_td$gondf)


gndf<-tapply(currency_td$gondf,currency_td$group,unique)

qrt<-tapply(currency_td$quarter,currency_td$group,unique)

yr<-tapply(currency_td$Year,currency_td$group,unique)

dfdate<-tapply(currency_td$diff_date,currency_td$group,mean)

dfmonth<-tapply(currency_td$Month,currency_td$group,unique)

dfmonth<-do.call(rbind,dfmonth)[,1]


dframe_sort<-data.frame(gndf,qrt,yr,dfdate,dfmonth,names(yr))

names(dframe_sort)<-c("gondf","quarter","Year","diff_date","month","group")


dframe_sort<-dframe_sort[dframe_sort$Year > 1393,]


dframe_sort$time<-1:nrow(dframe_sort)


dframe_sort$export<-ifelse(dframe_sort$gondf==23|dframe_sort$gondf==22,1,0)

dframe_sort$export<-ifelse(is.na(dframe_sort$export),0,dframe_sort$export)

dframe_sort$lag_export[2:nrow(dframe_sort)]<-dframe_sort$export[1:(nrow(dframe_sort)-1)]




####################################################################################################################################


#write.dta(currency_td,"test_currency2.dta")

#write.dta(dframe_sort,"sort.dta")

####################################################################################################################################




cast<-read.csv("cataso.csv")


pub_debt<-tapply(cast$public.investment,cast$family.name,sum)

pub_debt<-data.frame(pub_debt,names(pub_debt))

names(pub_debt)<-c("pub_debt","surname")



debt<-tapply(cast$deductions,cast$family.name,sum)

debt<-data.frame(debt,names(debt))

names(debt)<-c("debt","surname")



private<-tapply(cast$private.investment,cast$family.name,sum)

private<-data.frame(private,names(private))

names(private)<-c("private","surname")


total<-tapply(cast$total,cast$family.name,sum)

total<-data.frame(total,names(total))

names(total)<-c("total","surname")





realestate<-tapply(cast$real.estate,cast$family.name,sum)

realestate<-data.frame(realestate,names(realestate))

names(realestate)<-c("realestate","surname")





currency_td<-merge(currency_td,pub_debt,by="surname",all.x=T)

currency_td$pub_debt<-ifelse(is.na(currency_td$pub_debt),0,currency_td$pub_debt)


currency_td<-merge(currency_td,debt,by="surname",all.x=T)

currency_td$debt<-ifelse(is.na(currency_td$debt),0,currency_td$debt)



currency_td<-merge(currency_td,private,by="surname",all.x=T)

currency_td$private<-ifelse(is.na(currency_td$private),0,currency_td$private)



currency_td<-merge(currency_td,total,by="surname",all.x=T)

currency_td$total<-ifelse(is.na(currency_td$total),0,currency_td$total)


currency_td<-merge(currency_td,realestate,by="surname",all.x=T)

currency_td$realestate<-ifelse(is.na(currency_td$realestate),0,currency_td$realestate)



currency_td<-data.frame(currency_td)

currency_td<-currency_td[order(currency_td$time),]

write.csv(currency_td,"test_currency3.csv")

save.dta13(read.csv("test_currency3.csv"),"test_currency3.dta")




dfdebt<-tapply(currency_td$debt,currency_td$group,unique)

dfyr<-tapply(currency_td$Year,currency_td$group,unique)

dfpubdebt<-tapply(currency_td$pub_debt,currency_td$group,unique)

dfprivate<-tapply(currency_td$private,currency_td$group,unique)

dftotal<-tapply(currency_td$total,currency_td$group,unique)


dfrealestate<-tapply(currency_td$realestate,currency_td$group,unique)

dfgndf<-tapply(currency_td$gondf,currency_td$group,unique)

dfqrt<-tapply(currency_td$quarter,currency_td$group,unique)

dfmonth<-tapply(currency_td$Month,currency_td$group,unique)

dfmonth<-do.call(rbind,dfmonth)[,1]

dfdate<-tapply(currency_td$diff_date,currency_td$group,mean)

dfname<-tapply(currency_td$surname,currency_td$group,unique)



dfsort2<-data.frame(dfgndf,dfqrt,dfdebt,dfpubdebt,dfprivate,dftotal,dfyr,names(dfyr),dfmonth,dfdate,dfrealestate,dfname)


names(dfsort2)<-c("gondf","quarter","debt","pub_debt","private","total","year","time","month","diff_date","real_estate","dfname")



scrutinies<-c(1385, 1391, 1393,  1398,  1400,1406,1411,1414)



dfsort2$scrutiny<-NA

for(i in 1:length(scrutinies)){
	
	
dfsort2$scrutiny<-ifelse(dfsort2$year > scrutinies[i],scrutinies[i],dfsort2$scrutiny)	
	
	
}




sorts2<-list()


sorts<-table(dfsort2$dfname,dfsort2$scrutiny)


for(i in 1:ncol(sorts)){
  
  
  
  sorts2[[i]]<-data.frame(row.names(sorts), as.character(sorts[,i]), i )
  
  names(sorts2[[i]])<-c("name","chosen","scrut")
  
}



sorts2<-do.call(rbind,sorts2)

sorts2$chosen<-as.numeric(sorts2$chosen)

sorts2<-sorts2[sorts2$name != "",]

sorts2$name<-as.factor(sorts2$name)

save.dta13(sorts2,file="family_sorts.dta")

##########################################


probs_sort<-rep(NA,7)

for(i in 2:8){

sub<-sorts2[sorts2$chosen > 0 & sorts2$scrut==i,]

sub_0<-sorts2[sorts2$chosen > 0 & sorts2$scrut==(i-1),]

probs_sort[i-1]<-sum(sub$name %in% sub_0$name)/nrow(sub)

}


dfsort2<-dfsort2[dfsort2$year > 1393, ]

dfsort2$time<-as.numeric(dfsort2$time)

save.dta13(dfsort2,"sort2.dta")


